from django.db import models
from auths.models import User
from utils.models import CoreModel


# Create your models here.


class AppDetails(CoreModel):
    logo = models.TextField(verbose_name="应用logo", help_text="应用logo")
    appName = models.CharField(max_length=50, verbose_name="应用名称", help_text="应用名称")
    appleId = models.CharField(max_length=50, null=True, blank=True, verbose_name="appleId", help_text="appleId")
    description = models.CharField(max_length=255, default=None, null=True, blank=True,
                                   verbose_name="应用描述", help_text="应用描述")
    preview = models.JSONField(verbose_name="预览截图", help_text="预览截图")
    type = models.CharField(max_length=25, default="APK", verbose_name="应用类型", help_text="应用类型")
    appVersion = models.ForeignKey(to="AppVersion", default=None, null=True, blank=True,
                                   on_delete=models.CASCADE, verbose_name="应用版本ID", help_text="应用版本ID")
    versionCode = models.CharField(max_length=20, default=None, null=True, blank=True,
                                   verbose_name="应用版本号", help_text="应用版本号")
    user = models.ForeignKey(to=User, on_delete=models.CASCADE, verbose_name="用户ID", help_text="用户ID")

    class Meta:
        db_table = "application_app_details"
        verbose_name = 'APP热更新应用详情表'
        verbose_name_plural = verbose_name
        ordering = ('-update_time', 'sort')


class AppVersion(CoreModel):
    versionCode = models.CharField(max_length=20, verbose_name="版本号", help_text="版本号")
    description = models.TextField(default=None, null=True, blank=True, verbose_name="更新说明", help_text="更新说明")
    HBuilderUpdate = models.BooleanField(default=False, verbose_name="HBuilderX是否更新", help_text="HBuilderX是否更新")
    ForceUpdate = models.BooleanField(default=False, verbose_name="是否强制更新", help_text="是否强制更新")
    formalVersion = models.BooleanField(default=False, verbose_name="是否为正式版本", help_text="是否为正式版本")
    apkUrl = models.CharField(max_length=255, verbose_name="apk下载地址", help_text="apk下载地址")
    wgtUrl = models.CharField(max_length=255, verbose_name="wgt下载地址", help_text="wgt下载地址")
    app = models.ForeignKey(to=AppDetails, default=None, null=True, blank=True, on_delete=models.CASCADE,
                            verbose_name="应用详情ID", help_text="应用详情ID")

    class Meta:
        db_table = "application_app_version"
        verbose_name = 'APP热更新应用版本表'
        verbose_name_plural = verbose_name
        ordering = ('-create_time', 'sort')


class AppLog(CoreModel):
    appName = models.CharField(max_length=50, default=None, null=True, blank=True,
                               verbose_name="应用名称", help_text="应用名称")
    uniAppId = models.CharField(max_length=50, default=None, null=True, blank=True,
                                verbose_name="应用标识AppID", help_text="应用标识AppID")
    uniAppRemark = models.CharField(max_length=50, default=None, null=True, blank=True,
                                    verbose_name="应用标识备注", help_text="应用标识备注")
    appVersionName = models.CharField(max_length=50, default=None, null=True, blank=True,
                                      verbose_name="应用版本名称", help_text="应用版本名称")
    appVersionCode = models.CharField(max_length=50, default=None, null=True, blank=True,
                                      verbose_name="应用版本号", help_text="应用版本号")
    brand = models.CharField(max_length=50, default=None, null=True, blank=True,
                             verbose_name="品牌", help_text="品牌")
    systemName = models.CharField(max_length=50, default=None, null=True, blank=True,
                                  verbose_name="系统名称", help_text="系统名称")
    app = models.ForeignKey(to=AppDetails, on_delete=models.CASCADE, verbose_name="应用详情ID", help_text="应用详情ID")

    class Meta:
        db_table = "application_app_log"
        verbose_name = '应用版本日志表'
        verbose_name_plural = verbose_name
        ordering = ('-create_time', 'sort')


class AppUploadFile(CoreModel):
    name = models.TextField(verbose_name="文件原名", help_text="文件原名")
    size = models.IntegerField(verbose_name="原文件大小", help_text="原文件大小")
    suffix = models.CharField(max_length=10, verbose_name="原文件后缀", help_text="原文件后缀")
    baseDir = models.TextField(default=None, null=True, blank=True, verbose_name="项目根目录", help_text="项目根目录")
    fileDir = models.TextField(default=None, null=True, blank=True, verbose_name="文件绝对路径",
                               help_text="文件绝对路径")
    path = models.TextField(verbose_name="文件所在路径", help_text="文件所在路径")
    url = models.URLField(verbose_name="文件地址", help_text="文件地址")
    user = models.ForeignKey(to=User, null=True, blank=True, on_delete=models.CASCADE, verbose_name="上传用户ID",
                             help_text="上传用户ID")

    class Meta:
        db_table = "application_upload_file"
        verbose_name = 'APP热更新文件表'
        verbose_name_plural = verbose_name
        ordering = ('-create_time', 'sort')


class AppConfig(CoreModel):
    scheme = models.CharField(max_length=20, null=True, blank=True, verbose_name="请求协议",
                              help_text="请求协议")
    host = models.CharField(max_length=50, null=True, blank=True, verbose_name="服务器主机",
                            help_text="服务器主机")
    baseDir = models.TextField(default=None, null=True, blank=True, verbose_name="项目所在根目录",
                               help_text="项目所在根目录")
    meta = models.TextField(default=None, null=True, blank=True, verbose_name="配置信息", help_text="配置信息")

    class Meta:
        db_table = "application_config"
        verbose_name = 'APP热更新配置表'
        verbose_name_plural = verbose_name
        ordering = ('-create_time', 'sort')
